<?php


namespace app\api\middleware;


use think\Request;

/**
 * 请求频率验证
 * Class FrequencyCheck
 * @package app\wechat\middleware
 */
class FrequencyCheck
{
    public function handle(Request $request, \Closure $next)
    {
        $cache_name = $request->url().'request_time'.'-'.($request->header('token','nologin'));
        $last_request = cache($cache_name) ?? (time()-10);
        if ((time() - $last_request) < 1) {
            api_error('请勿重复请求');
        } else {
            cache($cache_name,time());
        }
        return $next($request);
    }

}